package com.itheima;

import javax.servlet.MultipartConfigElement;

import org.mybatis.spring.annotation.MapperScan;
import org.redisson.Redisson;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;

import com.itheima.utils.IdWorker;
import com.itheima.utils.JwtUtils;

import lombok.extern.slf4j.Slf4j;

/**
 * @Date :2019-08-14 14:32
 * @Author : yuan_he_song
 */
@SpringBootApplication
@MapperScan("com.itheima.mapper")
@EnableScheduling
@Slf4j
public class Application extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
        log.info("<==============*********服务启动成功*********==============>");
    }

    @Value("${spring.redis.host}")
    private String host;

    @Bean
    public IdWorker idWorker(){
        return new IdWorker();
    }
    @Bean
    public JwtUtils jwtUtils(){
        return new JwtUtils();
    }

     @Bean
     public Redisson redisson(){
         Config config = new Config();
         // 此为单机模式
         config.useSingleServer().setAddress("redis://"+host+":6379").setDatabase(0);
         // 集群模式
 //        config.useClusterServers()
 //                .addNodeAddress("redis://127.0.0.1:6380")
 //                .addNodeAddress("redis://127.0.0.1:6381")
 //                .addNodeAddress("redis://127.0.0.1:6382");
         return (Redisson) Redisson.create(config);
     }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(Application.class);
    }

    @Bean
    public MultipartConfigElement configElement(){
        MultipartConfigFactory factory = new MultipartConfigFactory();
        /** 单个文件大小 */
        factory.setMaxFileSize("10MB");
        /** 总上传数据大小 */
        factory.setMaxRequestSize("100MB");
        return factory.createMultipartConfig();
    }

}
